
function getFloorSpriteValueOfType(floor, row, col, spriteType)
	if (spriteType == "tile") then
		local tile = floorRef(floor, "tile", row, col)
		if (tile == nil) then
			return -1
		else
			return tile + 3
		end
	else
		local mob = floorRef(floor, "mob", row, col)
		if (mob == nil or mob == 0) then
			return -1
		else
			return mob + 258
		end
	end
end

function drawFloorSpritesOfType(floor, spriteType)
	local player = floor["player"]
	local centerX = player["col"]
	local centerY = player["row"]
	local xStart = centerX - 8
	local yStart = centerY - 5
	local xEnd = xStart + 15
	local yEnd = yStart + 9
	local screenX = 0
	local screenY = 0
	for j=yStart,yEnd do
		screenX = 0
		for i=xStart,xEnd do
			local spriteValue = getFloorSpriteValueOfType(floor, j, i, spriteType)
			if (spriteValue >= 0) then
				spr(getFloorSpriteValueOfType(floor, j, i, spriteType), screenX, screenY, 0, 2, 0, 0, 1, 1)
			end
			screenX = screenX + 16
		end
		screenY = screenY + 16
	end
end

function drawFloorTileSprites(floor)
	drawFloorSpritesOfType("tile")
end

function drawFloorMobSprites(floor)
	drawFloorSpritesOfType("mob")
end

function refreshOutput()
	cls(1)

	if (globalState["currentFloor"] ~= nil) then
		drawFloorSpritesOfType(globalState["currentFloor"], "tile")
		drawFloorSpritesOfType(globalState["currentFloor"], "mob")
	end
end

local timeHeld = {0,0,0,0,0,0,0,0}

function processButtonHold(key)
	return 0
end

function processButtonPress(key)
	if (key == 0) then
		attemptToMove(globalState, "up")
	elseif (key == 1) then
		attemptToMove(globalState, "down")
	elseif (key == 2) then
		attemptToMove(globalState, "left")
	elseif (key == 3) then
		attemptToMove(globalState, "right")
	end
end

function TIC()
	refreshOutput()

	for i=0,7 do
		if (btn(i)) then
			if (timeHeld[i] > 30) then
				processButtonHold(i)
			elseif (timeHeld[i] == 0) then
				processButtonPress(i)
			end
			timeHeld[i] = timeHeld[i] + 1
		else
			timeHeld[i] = 0
		end
	end
end
